package cn.edu.scau.cmi.hombio.service;

import cn.edu.scau.cmi.hombio.dao.CureMedicineDAO;
import cn.edu.scau.cmi.hombio.dao.CureMethodDAO;
import cn.edu.scau.cmi.hombio.dao.MedicineDAO;
import cn.edu.scau.cmi.hombio.dao.UnitDAO;

import cn.edu.scau.cmi.hombio.domain.CureMedicine;
import cn.edu.scau.cmi.hombio.domain.CureMethod;
import cn.edu.scau.cmi.hombio.domain.Medicine;
import cn.edu.scau.cmi.hombio.domain.Unit;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for CureMedicine entities
 * 
 */

@Service("CureMedicineService")

@Transactional
public class CureMedicineServiceImpl implements CureMedicineService {

	/**
	 * DAO injected by Spring that manages CureMedicine entities
	 * 
	 */
	@Autowired
	private CureMedicineDAO cureMedicineDAO;

	/**
	 * DAO injected by Spring that manages CureMethod entities
	 * 
	 */
	@Autowired
	private CureMethodDAO cureMethodDAO;

	/**
	 * DAO injected by Spring that manages Medicine entities
	 * 
	 */
	@Autowired
	private MedicineDAO medicineDAO;

	/**
	 * DAO injected by Spring that manages Unit entities
	 * 
	 */
	@Autowired
	private UnitDAO unitDAO;

	/**
	 * Instantiates a new CureMedicineServiceImpl.
	 *
	 */
	public CureMedicineServiceImpl() {
	}

	/**
	 * Save an existing CureMethod entity
	 * 
	 */
	@Transactional
	public CureMedicine saveCureMedicineCureMethod(Integer id, CureMethod related_curemethod) {
		CureMedicine curemedicine = cureMedicineDAO.findCureMedicineByPrimaryKey(id, -1, -1);
		CureMethod existingcureMethod = cureMethodDAO.findCureMethodByPrimaryKey(related_curemethod.getId());

		// copy into the existing record to preserve existing relationships
		if (existingcureMethod != null) {
			existingcureMethod.setId(related_curemethod.getId());
			existingcureMethod.setMemo(related_curemethod.getMemo());
			related_curemethod = existingcureMethod;
		}

		curemedicine.setCureMethod(related_curemethod);
		related_curemethod.getCureMedicines().add(curemedicine);
		curemedicine = cureMedicineDAO.store(curemedicine);
		cureMedicineDAO.flush();

		related_curemethod = cureMethodDAO.store(related_curemethod);
		cureMethodDAO.flush();

		return curemedicine;
	}

	/**
	 */
	@Transactional
	public CureMedicine findCureMedicineByPrimaryKey(Integer id) {
		return cureMedicineDAO.findCureMedicineByPrimaryKey(id);
	}

	/**
	 * Delete an existing CureMethod entity
	 * 
	 */
	@Transactional
	public CureMedicine deleteCureMedicineCureMethod(Integer curemedicine_id, Integer related_curemethod_id) {
		CureMedicine curemedicine = cureMedicineDAO.findCureMedicineByPrimaryKey(curemedicine_id, -1, -1);
		CureMethod related_curemethod = cureMethodDAO.findCureMethodByPrimaryKey(related_curemethod_id, -1, -1);

		curemedicine.setCureMethod(null);
		related_curemethod.getCureMedicines().remove(curemedicine);
		curemedicine = cureMedicineDAO.store(curemedicine);
		cureMedicineDAO.flush();

		related_curemethod = cureMethodDAO.store(related_curemethod);
		cureMethodDAO.flush();

		cureMethodDAO.remove(related_curemethod);
		cureMethodDAO.flush();

		return curemedicine;
	}

	/**
	 * Delete an existing Unit entity
	 * 
	 */
	@Transactional
	public CureMedicine deleteCureMedicineUnit(Integer curemedicine_id, Integer related_unit_id) {
		CureMedicine curemedicine = cureMedicineDAO.findCureMedicineByPrimaryKey(curemedicine_id, -1, -1);
		Unit related_unit = unitDAO.findUnitByPrimaryKey(related_unit_id, -1, -1);

		curemedicine.setUnit(null);
		related_unit.getCureMedicines().remove(curemedicine);
		curemedicine = cureMedicineDAO.store(curemedicine);
		cureMedicineDAO.flush();

		related_unit = unitDAO.store(related_unit);
		unitDAO.flush();

		unitDAO.remove(related_unit);
		unitDAO.flush();

		return curemedicine;
	}

	/**
	 * Delete an existing Medicine entity
	 * 
	 */
	@Transactional
	public CureMedicine deleteCureMedicineMedicine(Integer curemedicine_id, Integer related_medicine_id) {
		CureMedicine curemedicine = cureMedicineDAO.findCureMedicineByPrimaryKey(curemedicine_id, -1, -1);
		Medicine related_medicine = medicineDAO.findMedicineByPrimaryKey(related_medicine_id, -1, -1);

		curemedicine.setMedicine(null);
		related_medicine.getCureMedicines().remove(curemedicine);
		curemedicine = cureMedicineDAO.store(curemedicine);
		cureMedicineDAO.flush();

		related_medicine = medicineDAO.store(related_medicine);
		medicineDAO.flush();

		medicineDAO.remove(related_medicine);
		medicineDAO.flush();

		return curemedicine;
	}

	/**
	 * Save an existing Medicine entity
	 * 
	 */
	@Transactional
	public CureMedicine saveCureMedicineMedicine(Integer id, Medicine related_medicine) {
		CureMedicine curemedicine = cureMedicineDAO.findCureMedicineByPrimaryKey(id, -1, -1);
		Medicine existingmedicine = medicineDAO.findMedicineByPrimaryKey(related_medicine.getId());

		// copy into the existing record to preserve existing relationships
		if (existingmedicine != null) {
			existingmedicine.setId(related_medicine.getId());
			existingmedicine.setName(related_medicine.getName());
			existingmedicine.setNumber(related_medicine.getNumber());
			existingmedicine.setCompany(related_medicine.getCompany());
			existingmedicine.setQuantityPerCase(related_medicine.getQuantityPerCase());
			existingmedicine.setQuantityPerBox(related_medicine.getQuantityPerBox());
			existingmedicine.setPhone(related_medicine.getPhone());
			existingmedicine.setMemo(related_medicine.getMemo());
			related_medicine = existingmedicine;
		}

		curemedicine.setMedicine(related_medicine);
		related_medicine.getCureMedicines().add(curemedicine);
		curemedicine = cureMedicineDAO.store(curemedicine);
		cureMedicineDAO.flush();

		related_medicine = medicineDAO.store(related_medicine);
		medicineDAO.flush();

		return curemedicine;
	}

	/**
	 * Return all CureMedicine entity
	 * 
	 */
	@Transactional
	public List<CureMedicine> findAllCureMedicines(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<CureMedicine>(cureMedicineDAO.findAllCureMedicines(startResult, maxRows));
	}

	/**
	 * Return a count of all CureMedicine entity
	 * 
	 */
	@Transactional
	public Integer countCureMedicines() {
		return ((Long) cureMedicineDAO.createQuerySingleResult("select count(o) from CureMedicine o").getSingleResult()).intValue();
	}

	/**
	 * Load an existing CureMedicine entity
	 * 
	 */
	@Transactional
	public Set<CureMedicine> loadCureMedicines() {
		return cureMedicineDAO.findAllCureMedicines();
	}

	/**
	 * Delete an existing CureMedicine entity
	 * 
	 */
	@Transactional
	public void deleteCureMedicine(CureMedicine curemedicine) {
		cureMedicineDAO.remove(curemedicine);
		cureMedicineDAO.flush();
	}

	/**
	 * Save an existing Unit entity
	 * 
	 */
	@Transactional
	public CureMedicine saveCureMedicineUnit(Integer id, Unit related_unit) {
		CureMedicine curemedicine = cureMedicineDAO.findCureMedicineByPrimaryKey(id, -1, -1);
		Unit existingunit = unitDAO.findUnitByPrimaryKey(related_unit.getId());

		// copy into the existing record to preserve existing relationships
		if (existingunit != null) {
			existingunit.setId(related_unit.getId());
			existingunit.setName(related_unit.getName());
			existingunit.setMemo(related_unit.getMemo());
			related_unit = existingunit;
		}

		curemedicine.setUnit(related_unit);
		related_unit.getCureMedicines().add(curemedicine);
		curemedicine = cureMedicineDAO.store(curemedicine);
		cureMedicineDAO.flush();

		related_unit = unitDAO.store(related_unit);
		unitDAO.flush();

		return curemedicine;
	}

	/**
	 * Save an existing CureMedicine entity
	 * 
	 */
	@Transactional
	public void saveCureMedicine(CureMedicine curemedicine) {
		CureMedicine existingCureMedicine = cureMedicineDAO.findCureMedicineByPrimaryKey(curemedicine.getId());

		if (existingCureMedicine != null) {
			if (existingCureMedicine != curemedicine) {
				existingCureMedicine.setId(curemedicine.getId());
				existingCureMedicine.setQuantity(curemedicine.getQuantity());
				existingCureMedicine.setMemo(curemedicine.getMemo());
			}
			curemedicine = cureMedicineDAO.store(existingCureMedicine);
		} else {
			curemedicine = cureMedicineDAO.store(curemedicine);
		}
		cureMedicineDAO.flush();
	}
}
